﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;

namespace DiveManager
{
    class DatabaseTools
    {
        protected DiveDatabaseEntities diveDatabase = new DiveDatabaseEntities();

        //public bool checkRemoteDatabase()
        //{
        //    SqlConnection remoteSqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["RemoteDatabase"].ConnectionString);

        //    bool remoteDatabaseIsOnline = false;
        //    try
        //    {
        //        remoteSqlConnection.Open();
        //        remoteDatabaseIsOnline = true;
        //    }
        //    catch (Exception)
        //    {
        //    }
        //    finally
        //    {
        //        remoteSqlConnection.Close();
        //    }

        //    return remoteDatabaseIsOnline;
        //}

        public bool checkDatabase()
        {
            SqlConnection localSqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalDatabase"].ConnectionString);

            bool localDatabaseIsOnline = false;
            try
            {
                localSqlConnection.Open();
                localDatabaseIsOnline = true;
            }
            catch (Exception)
            {
            }
            finally
            {
                localSqlConnection.Close();
            }

            return localDatabaseIsOnline;
        }

        public bool UserExists(string username)
        {
            IQueryable<User> findUsername = (from a in diveDatabase.User
                                             where a.Name == username
                                             select a);
            if (findUsername.Count() == 0) return false;
            else return true;
        }
    }
}
